<?php

namespace catchAdmin\basics\model;

use catchAdmin\basics\model\search\RateSearch;
use catcher\base\CatchModel;
use think\facade\Db;
/**
 *
 * @property int $id
 * @property string $source_currency
 * @property string $target_currency
 * @property int $tenant_id
 * @property int $creator_id
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 * @property float $rate
 * @property date $start_date
 * @property date $end_date
 */
class Rate extends CatchModel
{
    use RateSearch;
    
    public $field = [
        // 主键
        'id',
        // 源币种
        'source_currency',
        // 目表币种
        'target_currency',
        // 汇率
        'rate',
        // 生效日期
        'start_date',
        // 失效日期
        'end_date',
        // 商户
        'tenant_id',
        // 创建人
        'creator_id',
        // 创建时间
        'created_at',
        // 更新时间
        'updated_at',
        // 删除时间
        'deleted_at'
    ];
    
    public $name = 'db_rate';

    /**
     * 列表
     * @throws \think\db\exception\DbException
     * @return \think\Paginator
     */
    public function getList($tenant_id = 0)
    {
        $where = [];
        if($tenant_id != 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        return $this->catchSearch()
            ->order('id','desc')
            ->where($where)
            ->withoutField('tenant_id,creator_id,updated_at,deleted_at,created_at')
            ->paginate()
            ->each(function (&$item){
                $item['source_currency_text'] = Db::table('db_currency')
                    ->where([['deleted_at','=',0],['id','=',$item['source_currency']]])
                    ->value('currency_num');
                $item['target_currency_text'] = Db::table('db_currency')
                    ->where([['deleted_at','=',0],['id','=',$item['target_currency']]])
                    ->value('currency_num');
            });
    }
}